const app = getApp()

module.exports = {
  getIfLogin(_this){
    wx.checkSession({
      success(checkRes){
        wx.getStorage({
          key:'token',
          success(tokenRes){
            console.log(tokenRes)
            if(tokenRes.data==''){
              _this.setData({
                canIUse:false
              })
            }else{
              wx.getStorage({
                key:'userInfo',
                success(useRes){
                  if(useRes.data==''){
                    _this.setData({
                      hasUserInfo:false
                    })
                  }else{
                    _this.setData({
                      hasUserInfo:true,
                      canIUse:true,
                      ['userInfo.avatarUrl']:wx.getStorageSync('userInfo').avatarUrl,
                      ['userInfo.nickName']:wx.getStorageSync('userInfo').nickName
                    })
                  }
                },
                fail(useErr){
                  _this.setData({
                    hasUserInfo:false
                  })
                }
              })
            }
          },
          fail(tokenErr){
            console.log(tokenErr)
            _this.setData({
              canIUse:false
            })
          }
        })
      },
      fail(checkErr){
        _this.setData({
          hasUserInfo:false,
          canIUse:false
        })
      }
    })
  },
  //获取用户手机号
  getMobile(_this){
    let data = {}
    let mobile = ''
    app.request('ugc/current',data,'GET').then(res=>{
      wx.hideLoading()
      if (res.success) {
        _this.setData({
          userType:res.content.userType
        })
        wx.setStorageSync('type', res.content.userType)
        if (res.content.shareId != '')
          wx.setStorageSync('loginUserId', res.content.shareId)
          mobile = res.content.mobile
          if (res.content.userType == 1) {
            _this.setData({
              showHuiyuan: false
            })
          } else {
            _this.setData({
              showHuiyuan: true
            })
          };
          if (mobile != '' && mobile != null) {
            wx.setStorageSync('phone', mobile)
            _this.setData({
              phone:mobile
            })
          } else {
            wx.navigateTo({
              url:'/pages/other/loginregister/register'
            })
          }
          wx.hideLoading()
        
      }else{
        if (res.status=='-10001'){
          _this.setData({
            hasUserInfo:false
          })
          app.globalData.userInfo = null
          wx.setStorageSync('userInfo', '')
          wx.setStorageSync('token', '')
          wx.setStorageSync('code', '')
          wx.setStorageSync('logs', '')
          wx.hideLoading()
        }else{
          wx.hideLoading()
        }
        
      }
    })
  }
}